package com.house.generator;

import com.baomidou.mybatisplus.generator.config.po.TableField;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class CodeUtils {
    private String[] commonFields= new String[]{"F_CreateUserID","F_CreateDeptID","F_CreateUnitID","F_CreateTime","F_UpdateUserID","F_UpdateDeptID","F_UpdateUnitID","F_UpdateTime"};
    //首字母小写
    public String lowerFirst(String s) {
        if (Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }

    //首字母大写
    public String upperFirst(String s) {
        if (Character.isUpperCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
    }

    //取表主键字段对象（可能组合主键）
    public List<TableField> getPrimaryKeys(List<TableField> list) {
        List<TableField> result = new ArrayList<>();
        for (TableField field : list) {
            if(field.isKeyFlag())
                result.add(field);
        }
        return result;
    }

    //取指定列所对应的对象属性名首字母大写
    public String getUpperFieldName(String fieldName) {
        if (fieldName.startsWith("F_")) {
            return upperFirst(fieldName.replaceFirst("F_",""));
        }
        return upperFirst(fieldName);
    }

    //取指定列所对应的对象属性名首字母大写
    public String getLowerFieldName(String fieldName) {
        if (fieldName.startsWith("F_")) {
            return lowerFirst(fieldName.replaceFirst("F_",""));
        }
        return lowerFirst(fieldName);
    }

    //取指定属性字段名的表字段对象
    public TableField getLogicDeletedField(List<TableField> list,String fieldName){
        for (TableField field : list) {
            if(field.getPropertyName().equals(fieldName))
                return field;
        }
        return null;
    }

    public String fieldAlias(String fieldsStr, String alias,String separator){
        String[] fields = fieldsStr.split(",");
        StringBuilder builder = new StringBuilder();
        int i = 0;
        for (String field : fields){
            if(builder.length() > 0)
                builder.append(",");
            if(i > 0 && i % 5 == 0) {
                if(!StringUtils.isEmpty(separator)) {
                    builder.append("\" ").append(separator);
                }
                builder.append("\n\t\t");
                if(!StringUtils.isEmpty(separator)) {
                    builder.append("\"");
                }
            }
            builder.append(alias + "." + field.trim());
            String aliasField = getLowerFieldName(field.trim());
            if(!StringUtils.isEmpty(getLowerFieldName(field))) {
                builder.append(" AS " + aliasField);
            }
            i ++;
        }
        return builder.toString();
    }

    public String getVoPackage(String entityPackage) {
        if(StringUtils.isEmpty(entityPackage)) {
            return "";
        }
        return entityPackage.replaceAll("\\.entity",".vo");
    }

    public boolean isCommonField(String fieldName){
        return Arrays.asList(commonFields).contains(fieldName);
    }

    public String propertyTypeConvert(String propertyType){
        if(propertyType.equals("LocalDateTime"))
            return "Date";
        if(propertyType.equals("LocalDate"))
            return "Date";
        return propertyType;
    }
}
